package views;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Model;
import presenters.LoginPresenter;


public class LoginBoxPanel extends JPanel {
	private JTextField textField;
	private JTextField textField_1;
	private LoginPresenter myPresenter;
	JLabel lblLoggedInAs;

	/**
	 * Create the panel.
	 */
	public LoginBoxPanel() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		panel_1.add(panel, BorderLayout.CENTER);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		Component verticalGlue_3 = Box.createVerticalGlue();
		panel.add(verticalGlue_3);
		
		Component verticalStrut = Box.createVerticalStrut(300);
		panel.add(verticalStrut);
		
		Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
		panel.add(rigidArea);
		
		JLabel lblUsername = new JLabel("Username:");
		panel.add(lblUsername);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		panel.add(textField_1);
		
		JLabel lblPassword = new JLabel("Password:");
		panel.add(lblPassword);
		
		textField = new JTextField();
		textField.setColumns(10);
		panel.add(textField);
		
		JButton btnLogin = new JButton("Login");
		panel.add(btnLogin);
		btnLogin.addActionListener(new MyActionListener());
		
		JButton btnLogout = new JButton("Logout");
		panel.add(btnLogout);
		btnLogout.addActionListener(new MyActionListener2());
		
		Component verticalGlue_1 = Box.createVerticalGlue();
		panel_1.add(verticalGlue_1, BorderLayout.WEST);
		
		Component verticalGlue = Box.createVerticalGlue();
		panel_1.add(verticalGlue, BorderLayout.SOUTH);
		
		Component verticalGlue_2 = Box.createVerticalGlue();
		panel_1.add(verticalGlue_2, BorderLayout.EAST);

		myPresenter = new LoginPresenter(this);
		
		lblLoggedInAs = new JLabel("Logged in as:");
		add(lblLoggedInAs, BorderLayout.SOUTH);
		
		JLabel lblNewLabel = new JLabel("Current User Login");
		panel_1.add(lblNewLabel, BorderLayout.NORTH);
	}
	
	private class MyActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String username = textField_1.getText();
			String password = textField.getText();
			if (myPresenter.login(username, password) == 0)
			{
				lblLoggedInAs.setText("Logged in as:");
			}
			else
			{
				lblLoggedInAs.setText("Logged in as: " + Model.getCurrentUser() + "\t\t\t Membership: " + Model.getCurrentUserType());
			}
			
			
		}
		
	}
	
	private class MyActionListener2 implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Model.setCurrentUser(null);
			Model.setCurrentUserType(null);
			lblLoggedInAs.setText("Logged in as:");
		}
		
	}

}
